package com.example.lab05.controlller;

import com.example.lab05.entity.Member;
import com.example.lab05.entity.Product;
import com.example.lab05.service.ProductService;

import com.example.lab05.util.UploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("product")
public class ProductController extends BaseController{
    @Autowired
    private ProductService productService;
    @GetMapping("queryAll")
    public String queryAll(){
        return "product";
    }
    @PostMapping("create")
    @ResponseBody
    public boolean create(@RequestPart MultipartFile file,@RequestPart MultipartFile[] files,@RequestPart Product product){
        String path = "d:\\lab\\product";
        product.setPicture(UploadUtil.savePicture(file,path));
        product.setPictures(UploadUtil.savePicture(files,path));
        productService.create(product);

        return true;

    }
    @GetMapping("queryAllRecommended")
    @ResponseBody
    private List<Product> queryAllRecommended() {
        return productService.queryList("queryAllRecommended", null);
    }
    @GetMapping("detail/{id}")
    private String detail(@PathVariable BigInteger id, Model model) {
        Product product = productService.queryById(id);
        session.setAttribute("product",product);
        product.setPictures(product.getPictures().replaceAll("[\\[\\]\"\s]", ""));
        model.addAttribute("product", product);
        return "/detail";
    }
}
